package com.ruoyi.web.controller.mes;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.MesPlaceorderSetupinfo;
import com.ruoyi.web.domain.Placrorder.Setupinfo1;
import com.ruoyi.web.domain.Placrorder.Setupinfo2;
import com.ruoyi.web.domain.Placrorder.Setupinfo3;
import com.ruoyi.web.domain.mes.MesCustom;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.MesPlaceorderInfo;
import com.ruoyi.web.service.IMesPlaceorderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson2.JSON;

/**
 * 下单标准基本信息Controller
 * 
 * @author wxt
 * @date 2024-01-09
 */
@Api("生产纸张下单标准设置")
@RestController
@RequestMapping("/mes/info")
public class MesPlaceorderInfoController extends BaseController
{
    @Autowired
    private IMesPlaceorderInfoService mesPlaceorderInfoService;

    /**
     * 查询下单标准基本信息列表
     */
    @ApiOperation("获取下单标准基本信息")
    @GetMapping("/list")
    public TableDataInfo list(MesPlaceorderInfo mesPlaceorderInfo)
    {
        startPage();
        List<MesPlaceorderInfo> list = mesPlaceorderInfoService.selectMesPlaceorderInfoList(mesPlaceorderInfo);
        return getDataTable(list);
    }

    /**
     * 导出下单标准基本信息列表
     */
    @ApiOperation("导出下单标准基本信息列表")
    @Log(title = "下单标准基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesPlaceorderInfo mesPlaceorderInfo)
    {
        List<MesPlaceorderInfo> list = mesPlaceorderInfoService.selectMesPlaceorderInfoList(mesPlaceorderInfo);
        ExcelUtil<MesPlaceorderInfo> util = new ExcelUtil<MesPlaceorderInfo>(MesPlaceorderInfo.class);
        util.exportExcel(response, list, "下单标准基本信息数据");
    }

    @ApiOperation("导出下单标准设置明细")
    @PostMapping("/exportszmx")
    public AjaxResult exportszmx(@RequestBody Map<String,String> params)
    {
        String type = params.get("type");
        String jsonString = params.get("data");
        if (type.equals("11")) {
            List<Setupinfo1> list = JSON.parseArray(jsonString, Setupinfo1.class);
            ExcelUtil<Setupinfo1> util = new ExcelUtil<Setupinfo1>(Setupinfo1.class);
            return  util.exportExcel( list, "下单标准设置明细数据");
        }
        if (type.equals("12")) {
            List<Setupinfo2> list = JSON.parseArray(jsonString, Setupinfo2.class);
            ExcelUtil<Setupinfo2> util = new ExcelUtil<Setupinfo2>(Setupinfo2.class);
            return  util.exportExcel( list, "下单标准设置明细数据");
        }
        if (type.equals("13")) {
            List<Setupinfo3> list = JSON.parseArray(jsonString, Setupinfo3.class);
            ExcelUtil<Setupinfo3> util = new ExcelUtil<Setupinfo3>(Setupinfo3.class);
            return  util.exportExcel( list, "下单标准设置明细数据");
        }
        return  new AjaxResult();
    }
    /**
     * 导入设置明细
     */
    @Log(title = "设置明细", businessType = BusinessType.IMPORT)
    @ApiOperation("导入设置明细")
    @PostMapping("/importData")
    public TableDataInfo importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MesPlaceorderSetupinfo> util = new ExcelUtil<MesPlaceorderSetupinfo>(MesPlaceorderSetupinfo.class);
        List<MesPlaceorderSetupinfo> mesCustomList = util.importExcel(file.getInputStream());
        //String operName = getUsername();
        //String message = mesCustomService.importCustom(mesCustomList, updateSupport, operName);
        //return success(message);
        return getDataTable(mesCustomList);
    }

    /**
     * 下载设置信息模板
     */
    @ApiOperation("设置信息下载模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response,@RequestBody Map<String,String> params)
    {
        String type = params.get("type");
        //ArrayList<MesPlaceorderSetupinfo> da=new ArrayList<>();
        //MesPlaceorderSetupinfo d=new MesPlaceorderSetupinfo();
        //d.setRemark("1");
        if (type.equals("11")) {
            ExcelUtil<Setupinfo1> util = new ExcelUtil<Setupinfo1>(Setupinfo1.class);
            util.importTemplateExcel(response, "设置信息数据");
        }
        if (type.equals("12")) {
            ExcelUtil<Setupinfo2> util = new ExcelUtil<Setupinfo2>(Setupinfo2.class);
            util.importTemplateExcel(response, "设置信息数据");
        }
        if (type.equals("13")) {
            ExcelUtil<Setupinfo3> util = new ExcelUtil<Setupinfo3>(Setupinfo3.class);
            util.importTemplateExcel(response, "设置信息数据");
        }
    }




    /**
     * 获取下单标准基本信息详细信息
     */
    @ApiOperation("获取下单标准基本信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mesPlaceorderInfoService.selectMesPlaceorderInfoById(id));
    }

    @ApiOperation("根据条件获得加放比例")
    @PostMapping(value = "/getputratio")
    public BigDecimal getputratio(@RequestBody Map<String,Object> params)
    {
//        String machineId = (String) params.get("machineId");
//        BigDecimal orderNum = (BigDecimal) params.get("orderNum");
        return mesPlaceorderInfoService.selectPutratio(params);
    }

    /**
     * 获取机器类型是否启用
     */
    @ApiOperation("获取机器类型是否启用")
    @PostMapping(value = "/status")
    public AjaxResult getstatus(@RequestBody Map<String,String> params)
    {
        String type = params.get("machinetype");
        return success(mesPlaceorderInfoService.getstatus(type));
    }

    /**
     * 新增下单标准基本信息
     */
    @ApiOperation("新增下单标准基本信息")
    @Log(title = "下单标准基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesPlaceorderInfo mesPlaceorderInfo)
    {
        return toAjax(mesPlaceorderInfoService.insertMesPlaceorderInfo(mesPlaceorderInfo));
    }

    /**
     * 修改下单标准基本信息
     */
    @ApiOperation("修改下单标准基本信息")
    @Log(title = "下单标准基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesPlaceorderInfo mesPlaceorderInfo)
    {
        return toAjax(mesPlaceorderInfoService.updateMesPlaceorderInfo(mesPlaceorderInfo));
    }
    @ApiOperation("下单标准启用停用")
    @PostMapping(value = "/updatestatus")
    public AjaxResult updatestatus(@RequestBody MesPlaceorderInfo mesPlaceorderInfo)
    {
        return toAjax(mesPlaceorderInfoService.updatestatus(mesPlaceorderInfo));
    }
    @ApiOperation("查询机台是否被引用")
    @PostMapping(value = "/getmachine")
    public AjaxResult getmachine(@RequestBody Map<String, String> params)
    {
        String machineId = params.get("machineId");//订单编号
        int rows= mesPlaceorderInfoService.getmachine(machineId);
        return success(rows);
    }

    /**
     * 删除下单标准基本信息
     */
    @ApiOperation("删除下单标准基本信息")
    @Log(title = "下单标准基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesPlaceorderInfoService.deleteMesPlaceorderInfoByIds(ids));
    }
}
